package capricornus.components.movieeffect
{
	import capricornus.events.StageEvent;
	import capricornus.core.ClassManager;
	import capricornus.core.StageManager;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	public class MovieEffectManager
	{
		private static var _ins:MovieEffectManager = null;
		
		private var _isPlaying:Boolean = false;
		
		private var _topMask:Sprite;
		private var _bottomMask:Sprite;
		private var _txtJumpMovie:TextField;
		private var _clickContinue:Sprite;
		private var _currentSegment:MovieSegment;
		private var _blackMask:Shape;
		private var _container:Sprite;
		
		public function MovieEffectManager()
		{
			init();
		}
		
		public static function get instance():MovieEffectManager
		{
			if( null==_ins )
			{
				_ins = new MovieEffectManager();
			}
			return _ins;
		}
		
		private function init():void
		{
			_container = new Sprite();
			
			_topMask = ClassManager.getClassInstance("MovieEffectBackPanel") as Sprite;
			_bottomMask = new Sprite();
			var dis:Sprite = ClassManager.getClassInstance( "MovieEffectBackPanel" ) as Sprite;
			_bottomMask.addChild( dis );
			dis.y = -dis.height;
			
			_blackMask = new Shape();
			_blackMask.graphics.beginFill( 0, 0.05 );
			_blackMask.graphics.drawRect( 0, 0, 1, 1 );
			_blackMask.graphics.endFill();
			_container.addChild( _blackMask );
			
			_container.addChild( _topMask );
			_container.addChild( _bottomMask );
			
			_txtJumpMovie = new TextField();
			_txtJumpMovie.htmlText = '<u><font color="#F4F7C0">跳过剧情</font></u>';
			_txtJumpMovie.width = _txtJumpMovie.textWidth + 4;
			_txtJumpMovie.height = _txtJumpMovie.textHeight + 4;
			
			_container.addChild( _txtJumpMovie );
			_txtJumpMovie.x = 66;
			
			_clickContinue = ClassManager.getClassInstance("MovieEffectClickContinuePanel") as Sprite;
			_container.addChild( _clickContinue );
			
			_txtJumpMovie.addEventListener( MouseEvent.CLICK, onClickJump, false, 0, true );
		}
		
		public function playByEffectID( id:String ):void
		{
		}
		
		public function play( segment:MovieSegment ):void
		{
			if( _isPlaying ) return;
			
			//隐藏其他界面
			//
			
			StageManager.stage.addChild( _container );
			_container.mouseChildren = true;
			_container.mouseEnabled = true;
			_container.alpha = 1;
			_container.addChildAt( segment, 3 );
			
			_topMask.height = 0;
			_bottomMask.height = 0;
//			TweenLite.to(_topMask, 1, {height:120});
//			TweenLite.to(_bottomMask, 1, {height:120});
			_isPlaying = true;
			_currentSegment = segment;
			onResize( null );
			StageManager.stage.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown, false, 0, true );
			StageManager.instance.addEventListener( StageEvent.RESIZE, onResize, false, 0, true );
		}
		
		private function clear():void
		{
//			TweenLite.to( _container, 1.5, {alpha:0, onComplete:onFinish} );
			StageManager.stage.removeEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
		}
		
		private function onFinish():void
		{
			if( _currentSegment != null )
			{
				_currentSegment.destroy();
				_container.removeChild( _currentSegment );
				_currentSegment = null;
			}
			_isPlaying = false;
			StageManager.stage.removeChild( _container );
			StageManager.instance.removeEventListener( StageEvent.RESIZE, onResize );
		}
		
		private function onMouseDown( e:MouseEvent ):void
		{
			if( e.target != _txtJumpMovie )
			{
				e.stopImmediatePropagation();
				if( !_currentSegment.nextFrame() )
				{
					clear();
				}
				else
				{
					resizeClick();
				}
			}
		}
		
		private function onClickJump( e:MouseEvent ):void
		{
			e.stopImmediatePropagation();
			clear();
		}
		
		private function onResize( e:StageEvent ):void
		{
			_currentSegment.resize();
			_blackMask.width = StageManager.stageWidth;
			_blackMask.height = StageManager.stageHeight;
			_topMask.width = _bottomMask.width = StageManager.stageWidth;
			_bottomMask.y = StageManager.stageHeight;
			
			resizeClick();
		}
		
		private function resizeClick():void
		{
			if( !_currentSegment.isLeftSize() )
			{
				_txtJumpMovie.y = StageManager.stageHeight - 24;
				_txtJumpMovie.x = StageManager.stageWidth - _txtJumpMovie.width - 50;
				
				_clickContinue.x = 66;
				_clickContinue.y = StageManager.stageHeight - _clickContinue.height - 8;
			}
			else
			{
				_txtJumpMovie.x = 66;
				_txtJumpMovie.y = StageManager.stageHeight - 24;
				
				_clickContinue.x = StageManager.stageWidth - _clickContinue.width - 50;
				_clickContinue.y = StageManager.stageHeight - _clickContinue.height - 8;
			}
		}
	}
}




















